import json
import pandas as pd
import numpy as np

from sqlalchemy.orm import Session
from core.response import resp_500
from core.logger import logger
from datetime import datetime, timedelta

from app.fault_self_recovery.models.fsr_rabbitmq_queue import FSRRabbitmqQueue


class FSRRabbitmqQueueHpaHandler():
    """  FSRRabbitmqQueue Handler
    """
    def __init__(self):
        pass


    ##### 增删改查
    def get_rabbitmq_queue_search(self, db: Session, page: int, per_page: int, field: str, value: str):
        """
        根据表字段模糊匹配，获取rabbitmq队列配置信息
        """
        try:
            field_info = {field: value}
            order_info = {"classes": "asc", "name": "asc"}
            result = FSRRabbitmqQueue.filter_by_field_search(db, page, per_page, field_info, order_info)
            if not result:
                logger.error(f"get rabbitmq queue search, result is null => {field}:{value}")
                return False, None

        except Exception as e:
            message = f"get rabbitmq queue search by {field}:{value} error"
            logger.exception(f"{message} => {e}")
            return resp_500(message)

        return True, result




    def get_rabbitmq_queue_match(self, db: Session, page: int, per_page: int, field: str, value: str):
        """
        根据表字段完全匹配，获取rabbitmq队列配置信息
        """
        try:
            result = FSRRabbitmqQueue.filter_by_field_match(db, page, per_page, {field: value})
            if not result:
                logger.error(f"get rabbitmq queue match, result is null => {field}:{value}")
                return False

        except Exception as e:
            message = f"get rabbitmq queue match by {field}:{value} error"
            logger.exception(f"{message} => {e}")
            return resp_500(message)

        return result




    def add_rabbitmq_queue(self, db: Session, data: dict):
        """
        创建rabbitmq队列配置信息
        """
        try:
            rabbitmq_queue_object = FSRRabbitmqQueue.get_object_by_cluster_vhost_name(db, data["cluster"], data["vhost"],data["name"])
            if rabbitmq_queue_object:
                # 如果存在， 则异常返回
                logger.error(f"add rabbitmq queue failed, name:{data['name']} was exist")
                return False

            result = FSRRabbitmqQueue.add(db, data)
            if not result:
                logger.error(f"add rabbitmq queue to database failed")
                return False
        except Exception as e:
            message = f"add rabbitmq queue error"
            logger.exception(f"{message} data:{data} => {e}")
            return resp_500(message)

        return True



    def update_rabbitmq_queue(self, db: Session, data: dict):
        """
        修改rabbitmq队列配置信息
        """
        try:
            rabbitmq_queue_object = FSRRabbitmqQueue.get_object_by_uuid(db, data['uuid'])
            if not rabbitmq_queue_object:
                # 如果不存在， 则异常返回
                logger.error(f"update rabbitmq queue failed, uuid:{data['uuid']} not exist")
                return False

            # 更新队列信息
            result = FSRRabbitmqQueue.update(db, data)
            if not result:
                logger.error(f"update rabbitmq queue to database failed")
                return False
        except Exception as e:
            message = f"update rabbitmq queue error"
            logger.exception(f"{message} data:{data} => {e}")
            return resp_500(message)

        return True



    def delete_rabbitmq_queue(self, db: Session, data: dict):
        """
        删除rabbitmq队列配置信息
        """
        try:
            rabbitmq_queue_object = FSRRabbitmqQueue.get_object_by_uuid(db, data['uuid'])
            if not rabbitmq_queue_object:
                # 如果不存在， 则异常返回
                logger.error(f"delete rabbitmq queue failed, uuid:{data['uuid']} not exist")
                return False

            # 删除队列信息
            FSRRabbitmqQueue.delete_object_by_uuid(db, data['uuid'])
        except Exception as e:
            message = f"delete rabbitmq queue error"
            logger.exception(f"{message} data:{data} => {e}")
            return resp_500(message)

        return True



